package com.woyowodaows.activity.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;


import com.woyowodaows.R;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.home.RegisterDataBean;
import com.woyowodaows.bean.home.UserDataBean;
import com.woyowodaows.bean.mine.UserInfoBean;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.CacheUserInfoUtils;
import com.woyowodaows.utils.RegexpUtils;
import com.woyowodaows.utils.RegularUtils;
import com.woyowodaows.utils.SPUtils;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.CountdownButton;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 找回密码
 */

public class FindPasswordActivity extends BaseActivity {


    @BindView(R.id.et_find_pwd_phone)
    EditText etFindPwdPhone;
    @BindView(R.id.et_find_pwd_verification_code)
    EditText etFindPwdVerificationCode;
    @BindView(R.id.btn_find_pwd_get_verification_code)
    CountdownButton btnFindPwdGetVerificationCode;
    @BindView(R.id.et_find_new_pwd)
    EditText etFindNewPwd;
    @BindView(R.id.et_find_pwd_again_pwd)
    EditText etFindPwdAgainPwd;
    @BindView(R.id.iv_registere_clear)
    ImageView ivRegistereClear;
    @BindView(R.id.btn_find_pwd_submit)
    Button btnFindPwdSubmit;

    private String messageId;
//    private UserInfoBean userInfo;


    @Override
    public int getLayoutId() {
        return R.layout.activity_find_password;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
//        boolean isLogin = (boolean) SPUtils.get(mContext, SPUtils.IS_LOGIN, false);
//        if (isLogin){
//            setTitleBar(false, "修改密码", "", 0, 0, null);
//            userInfo= CacheUserInfoUtils.getUserInfo();
//            etFindPwdPhone.setEnabled(false);
//            etFindPwdPhone.setText(userInfo.getMobilePhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
//        }else {
            setTitleBar(false, "忘记密码", "", 0, 0, null);
//            etFindPwdPhone.setEnabled(true);
//        }

    }

    private void findPassword() {

        String mobileNumber;

//        if (userInfo != null) {
//            mobileNumber = userInfo.getMobilePhone();
//        }else {
            mobileNumber = etFindPwdPhone.getText().toString().trim();


        String password = etFindNewPwd.getText().toString().trim();
        String affirmPassword = etFindPwdAgainPwd.getText().toString().trim();
        String verificationCode = etFindPwdVerificationCode.getText().toString().trim();

        if (TextUtils.isEmpty(mobileNumber)){
            ToastUtil.show(mContext,"请输入手机号码");
            return;
        }

        if (!RegularUtils.getInstate().isMobileNO(mobileNumber)){
            ToastUtil.show(mContext,"请输入正确的手机号码");
            return;
        }


        if (TextUtils.isEmpty(verificationCode)){
            ToastUtil.show(mContext,"请输入短信验证码");
            return;
        }


        if (TextUtils.isEmpty(password)){
            ToastUtil.show(mContext,"请输入密码");
            return;
        }

        if (TextUtils.isEmpty(affirmPassword)){
            ToastUtil.show(mContext,"请再次输入密码");
            return;
        }
        if(!RegexpUtils.includeEnglishNumber(password)){
            ToastUtil.show(mContext, "输入的密码包含数字和英文");
            return;
        }
        if(!RegexpUtils.includeEnglishNumber(affirmPassword)){
            ToastUtil.show(mContext, "输入的密码包含数字和英文");
            return;
        }

        if (password.length() < 6 || affirmPassword.length() < 6){
            ToastUtil.show(mContext,"密码长度不能少于6位");
            return;
        }

        if (!password.equals(affirmPassword)){
            ToastUtil.show(mContext,"两次密码输入不一致");
            return;
        }
//        password:weixuehu1
//        messageSn:723897008162
//        mobilePhone:15207483103
//        verifyCode:261867

        Map<String,Object> mapParameter = new HashMap<>();
        mapParameter.put("mobilePhone",mobileNumber);
        mapParameter.put("password",password);
        mapParameter.put("verifyCode",verificationCode);
        mapParameter.put("messageSn",messageId);
        showProgressDialog(mContext,"正在找回密码...");
        HttpAsyncTask.getInstance().onPostParam(this, "正在修改密码...", false, InterfaceUrl.RESETPASSORD, UserDataBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    UserDataBean bean = (UserDataBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        ToastUtil.show_short(FindPasswordActivity.this, "找回密码成功");
                        finish();
                    } else {
                        ToastUtil.show_short(FindPasswordActivity.this, bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show_short(FindPasswordActivity.this, message);
                dismissProgressDialog();
            }
        });

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        HttpAsyncTask.getInstance().progressBarDialog = null;
    }



    @OnClick({R.id.btn_find_pwd_get_verification_code, R.id.iv_registere_clear, R.id.btn_find_pwd_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_find_pwd_get_verification_code:
                getMessageCode();
                break;
            case R.id.iv_registere_clear:
                break;
            case R.id.btn_find_pwd_submit:
                findPassword();
                break;
        }
    }



    //获取短信验证码
    private void getMessageCode() {

        String mobileNumber;

//        if (userInfo != null) {
//            mobileNumber = userInfo.getMobilePhone();
//        }else {
            mobileNumber = etFindPwdPhone.getText().toString().trim();
//        }

        if (TextUtils.isEmpty(mobileNumber)){
            ToastUtil.show(mContext,"请输入手机号码");
            return;
        }

        if (!RegularUtils.getInstate().isMobileNO(mobileNumber)){
            ToastUtil.show(mContext,"请输入正确的手机号码");
            return;
        }
        Map<String,Object> mapParameter = new HashMap<>();
        mapParameter.put("mobilePhone",mobileNumber);
        showProgressDialog(mContext,"验证码发送中...");
        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.SENDFORGETPASSWORDCODE, RegisterDataBean.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null){
                    RegisterDataBean bean = (RegisterDataBean) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS){
                        btnFindPwdGetVerificationCode.start();
                        messageId = bean.getData();
                        ToastUtil.show(mContext,"验证码发送成功");
                    }else {
                        ToastUtil.show(mContext,bean.getMessage());
                    }
                }
                dismissProgressDialog();
            }

            @Override
            public void onError(String message) {
                ToastUtil.show(mContext,message);
                dismissProgressDialog();
            }
        });

    }
}
